package com.siao.loader
{
	import com.siao.errors.SingletonError;
	import com.siao.events.BytesEvent;
	
	import flash.events.Event;
	
	/**
	 * 字节多线程加载器
	 * @author SiaoLeon
	 */
	public class BytesMultiLoader
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		/**
		 * 默认进程数
		 */
		private static const DEFAULT_PROCESS_NUMBER:uint = 6;
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		private static var _instance:BytesMultiLoader;
		
		/**
		 * 获取单例对象
		 */
		public static function get instance():BytesMultiLoader
		{
			if (_instance == null)
			{
				_instance = new BytesMultiLoader();
			}
			return _instance;
		}
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		/**
		 * 队列长度
		 * @return
		 */
		public function get length():uint
		{
			return this.queue.length;
		}
		
		private var _thread:uint;
		
		/**
		 * 加载线程数
		 * @return
		 */
		public function get thread():uint
		{
			return _thread;
		}
		
		public function set thread(value:uint):void
		{
			_thread = value;
			this.process();
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var queue:Vector.<BytesLoader>;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数，单例模式
		 */
		public function BytesMultiLoader()
		{
			if (_instance != null)
			{
				throw new SingletonError(this);
			}
			_instance = this;
			
			this.init();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 加载资源
		 * @param url 加载地址
		 * @param params 附带参数
		 * @return
		 */
		public function load(url:String, params:Object=null):BytesLoader
		{
			var loader:BytesLoader;
			
			for each (loader in this.queue)
			{
				if (loader.url == url)
				{
					return loader;
				}
			}
			
			loader = new BytesLoader(url, params);
			loader.addEventListener(BytesEvent.COMPLETE, onComplete)
			loader.addEventListener(BytesEvent.ERROR, onComplete);
			
			this.queue.push(loader);
			this.process();
			
			return loader;
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		/**
		 * 初始化
		 */
		private function init():void
		{
			this.queue = new Vector.<BytesLoader>();
			this.thread = DEFAULT_PROCESS_NUMBER;
		}
		
		/**
		 * 处理线程
		 */
		private function process():void
		{
			for (var i:uint = 0; i < this.thread; i++)
			{
				if (i >= this.queue.length)
				{
					break;
				}
				
				var loader:BytesLoader = this.queue[i];
				
				if (loader.connected)
				{
					continue;
				}
				
				loader.load();
			}
		}
		
		/**
		 * 加载完成时
		 * @param event
		 */
		private function onComplete(event:Event):void
		{
			var index:int = this.queue.indexOf(event.target as BytesLoader);
			this.queue.splice(index, 1);
		}
	
	}
}
